package team.umbc;

import com.facebook.android.*;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CheckinActivity extends Activity implements OnClickListener {

	private Facebook facebook;
	private LinearLayout placeList;
	private PlacesDB placesDB;
	private SQLiteDatabase db;
	private Cursor cursor;
	private SharedPreferences settings;
	boolean firstTime;
	
	public final static String APP_ID = "209786082402554";
	public final static String KEY = "facebook";
	public final static String ACCESS_TOKEN = "AccessToken";
	public final static String EXPIRES_IN = "ExpiresIn";
	public final static String PREFS_NAME = "Check-in";

	public void onCreate(Bundle savedInstance) {
		//Set up 
		super.onCreate(savedInstance);
		setContentView(R.layout.checkin);
		placeList = (LinearLayout) findViewById(R.id.list);

		//Setting up the database
		placesDB = new PlacesDB(this);
		db = placesDB.getReadableDatabase();
		startService(new Intent(this, PlaceService.class));
		cursor = db.query(PlacesDB.TABLE, null, null, null, null, null, null);
		startManagingCursor(cursor);
		if (cursor.moveToFirst()) {
			initButtons();
			
		}
		settings = getSharedPreferences(PREFS_NAME, 0);
		//Setting up Facebook
		facebook = new Facebook(APP_ID);
		facebook.setAccessToken(settings.getString("AccessToken", ""));
		facebook.setAccessExpires(0);
		
		if (!facebook.isSessionValid()) {
			firstTimeDialog();
		}
	}

	private void firstTimeDialog() {
		startActivity(new Intent(this, FirstTime.class));
		// Bundle services = getIntent().getExtras();
		/*
		 * if(services != null){ facebook =
		 * (Facebook)services.getSerializable("facebook"); } if(facebook !=
		 * null){ firstTime = false; Editor editor = settings.edit();
		 * editor.putBoolean("firstTime", firstTime); editor.commit(); }
		 */
	}

	private void initButtons() {
		Button temp;
		int nameIndex = cursor.getColumnIndex(PlacesDB.P_NAME);
		int tagIndex = cursor.getColumnIndex(PlacesDB.P_TAG);
		for (int i = 0; i < 5; i++) {
			temp = new Button(this);
			temp.setText(cursor.getString(nameIndex));
			temp.setId(cursor.getInt(tagIndex));
			temp.setOnClickListener(this);

			placeList.addView(temp);
			cursor.moveToNext();
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		facebook.authorizeCallback(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View v) {
		Editor editor = settings.edit();
		cursor.moveToFirst();
		switch (v.getId()) {
		case 100:
		case 101:
		case 102:
		case 103:
		case 104:
			String name = ((Button) v).getText().toString();
			Cursor c1 = db.query(PlacesDB.TABLE, 
					new String[] {PlacesDB.P_NAME, PlacesDB.P_LONG, PlacesDB.P_LAT, PlacesDB.P_FB_ID}, 
					PlacesDB.P_NAME + " = '" + name + "'",
					null, null, null, null);
			
			int longIndex = c1.getColumnIndex(PlacesDB.P_LONG);
			int latIndex = c1.getColumnIndex(PlacesDB.P_LAT);
			int fbID = c1.getColumnIndex(PlacesDB.P_FB_ID);
			c1.moveToFirst();
			
			System.out.println(c1.getString(fbID));
			editor.putString("checkin", name);
			editor.putString("long", c1.getString(longIndex));
			editor.putString("lat", c1.getString(latIndex));
			editor.putString("fbID", c1.getString(fbID));
			editor.commit();
			Intent i = new Intent(this, CheckoutActivity.class);
//			i.putExtra("facebook", facebook);
			startActivity(i);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		Editor editor = settings.edit();
		editor.putString("friend", "");
		editor.commit();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		cursor.close();
		db.close();
	}

}
